package model;

import java.util.Comparator;
import java.util.Random;

public final class Contact implements Comparator < Contact >{
	int ID;
	String firstname;
	String lastname;
	String address;
	String companyaddress;
	int personalphone;
	int professionalphone;
	String homepage;
	String proemail; 
	String peremail;
	String photo;
	String group;

	public Contact(){
		ID=0;
		firstname=null;
		lastname=null;
		address=null;
		companyaddress=null;
		personalphone=000000000;
		professionalphone=000000000;
		homepage=null;
		proemail=null; 
		peremail=null;
		group=null;
		photo=null;
	}
	public Contact(int nID){
		Random rand=new Random();
		ID=nID;
		firstname=RandomString();
		lastname=RandomString();
		address=RandomString();
		companyaddress=RandomString();
		personalphone=Integer.parseInt(RandomInt());
		professionalphone=Integer.parseInt(RandomInt());
		homepage="www."+RandomString()+".com";
		proemail=lastname+"@ece.fr"; 
		peremail=firstname+"."+lastname+"@gmail.com";
		group=RandomGroup();
		photo="./res/"+rand.nextInt(10)+".png";
	}

	String RandomString(){
		Random random=new Random();
		String fullword;
		char[] word = new char[random.nextInt(8)+3];
		for(int j = 0; j < word.length; j++)
		{
			word[j] = (char)('a' + random.nextInt(26));
		}
		fullword = new String(word);
		return fullword;
	}

	String RandomInt(){
		Random random=new Random();
		String fullint="06";
		int currentint;
		for(int j = 0; j <=3; j++)
		{
			currentint= (int) (10+(Math.random()*(99-10+1)));
			fullint+=String.valueOf(currentint);
		}
		return fullint;
	}
	
	String RandomGroup(){
		String[]groups={"Bosses","Family","Coworkers","Friends","Enemies"};
		int choice=(int) (Math.random()*4);
		return groups[choice];
	}

	public String getGroup() {
		return group;
	}
	public void setGroup(String group) {
		this.group = group;
	}
	Contact(String nfirstname,String nlastname,String naddress,String ncompanyaddress,int npersonalphone,int nprofessionalphone,String nhomepage,String nproemail,String nperemail,String ngroup,String nphoto){
		this.firstname=nfirstname;
		this.lastname=nlastname;
		this.address=naddress;
		this.companyaddress=ncompanyaddress;
		this.personalphone=npersonalphone;
		this.professionalphone=nprofessionalphone;
		this.homepage=nhomepage;
		this.proemail=nproemail; 
		this.peremail=nperemail;
		this.group=ngroup;
		this.photo=nphoto;
	}
	
	Contact(int id,String nfirstname,String nlastname,String naddress,String ncompanyaddress,int npersonalphone,int nprofessionalphone,String nhomepage,String nproemail,String nperemail,String ngroup,String nphoto){
		this.ID=id;
		this.firstname=nfirstname;
		this.lastname=nlastname;
		this.address=naddress;
		this.companyaddress=ncompanyaddress;
		this.personalphone=npersonalphone;
		this.professionalphone=nprofessionalphone;
		this.homepage=nhomepage;
		this.proemail=nproemail; 
		this.peremail=nperemail;
		this.group=ngroup;
		this.photo=nphoto;
	}

	public int getID() {
		return ID;
	}
	public void setID(int iD) {
		ID = iD;
	}

	@Override
	public int compare(Contact o1, Contact o2) {
		if(o1.getLastname() != null && o2.getLastname() != null){
			return o1.getLastname().compareTo(o2.getLastname());
		}

		return 0;
	}
	public String showContact(){
		StringBuilder builder=new StringBuilder();
		builder.append("Contact n");builder.append(ID);
		builder.append("\n Mr."); builder.append( firstname);
		builder.append(" "); builder.append( lastname);
		builder.append("\naddress: "); builder.append( address);
		builder.append("\ncompaddress: "); builder.append( companyaddress);
		builder.append("\npersonal phone: "); builder.append(personalphone);
		builder.append("\nprofessional phone: "); builder.append(professionalphone);
		builder.append("\nhomepage:"); builder.append( homepage);
		builder.append("\nprofessional email: "); builder.append( proemail);
		builder.append("\npersonal email: "); builder.append( peremail);
		builder.append("\ngroup: "); builder.append( group);
		builder.append("\ngroup: "); builder.append( photo);
		builder.append("\n \n");
		
		return (builder.toString());
	}
	

	public String saveCSVContact(){
		StringBuilder retour =new StringBuilder();
		retour.append(ID);
		retour.append(";");
		retour.append(firstname);
		retour.append(";");
		retour.append(lastname);
		retour.append(";");
		retour.append(address);
		retour.append(";");
		retour.append(companyaddress);
		retour.append(";");
		retour.append(personalphone);
		retour.append(";");
		retour.append(professionalphone);
		retour.append(";");
		retour.append(homepage);
		retour.append(";");
		retour.append(proemail);
		retour.append(";");
		retour.append(peremail);
		retour.append(";");
		retour.append(group);
		retour.append(";");
		retour.append(photo);
		return(retour.toString());
	}

	public void loadContact(){

	}

	public String getFirstname() {
		return firstname;
	}

	public void setFirstname(String firstname) {
		this.firstname = firstname;
	}

	public String getLastname() {
		return lastname;
	}

	public void setLastname(String lastname) {
		this.lastname = lastname;
	}

	public String getAddress() {
		return address;
	}

	public void setAddress(String address) {
		this.address = address;
	}

	public String getCompanyaddress() {
		return companyaddress;
	}

	public void setCompanyaddress(String companyaddress) {
		this.companyaddress = companyaddress;
	}

	public int getPersonalphone() {
		return personalphone;
	}

	public void setPersonalphone(int personalphone) {
		this.personalphone = personalphone;
	}

	public int getProfessionalphone() {
		return professionalphone;
	}

	public void setProfessionalphone(int professionalphone) {
		this.professionalphone = professionalphone;
	}

	public String getHomepage() {
		return homepage;
	}

	public void setHomepage(String homepage) {
		this.homepage = homepage;
	}

	public String getProemail() {
		return proemail;
	}

	public void setProemail(String proemail) {
		this.proemail = proemail;
	}

	public String getPeremail() {
		return peremail;
	}

	public void setPeremail(String peremail) {
		this.peremail = peremail;
	}

	public String getPhoto() {
		return photo;
	}

	public void setPhoto(String photo) {
		this.photo = photo;
	}
}
